%
% Author: Vasyl Mykhalchuk
% 01/02/2012
%

function [ dist ] = vmDijkstra( xMeshGraph, xAdjVertList, xSource )
% Initializations
n = xMeshGraph.mMesh.mNumberV;
distanceMatrix = xMeshGraph.mDistanceMatrix;
dist(1:n) = Inf; 
dist(xSource) = 0;

% Unvisited Set % All unvisited
unvisited(1:n) = 1;


while ( isNotEmpty(unvisited) )
    u = getClosestVert( unvisited, dist );
    if ( u == -1 )
        unvisited(1:n) = 0;
        continue;
    end % if
    % All remaining vertices are inaccessible from the source
    if ( dist(u) == Inf )
        unvisited(1:n) = 0;
        continue;
    end % if
    
    % Remove u from _unvisited set_ 
    unvisited(u) = 0;
    
    % For each neighbor of u
    [neighbors, numNeighbors] = getNeighbors(xAdjVertList, u);
    for i = 1:numNeighbors
        v = neighbors(i);
        alt = dist(u) + distanceMatrix(u, v);
        if ( alt < dist(v) )
            dist(v) = alt;
        end % if
    end % for
    
end % while

% return dist

end % vmDijkstra


function num = unvisSetSize( unvisited )
    num = 0;
    n = size(unvisited, 2);
    for i = 1:n
        if ( unvisited(i) > 0 )
            num = num + 1;
        end % if
    end % for
end % function